package cn.com.cennavi.test;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;

import cn.com.cennavi.bean.ShpFileBean;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.PrecisionModel;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

/**
 * 读取shp文件
 * 
 * @ClassName: ShpNew
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author buf
 * @date 2017年2月10日 下午1:12:10
 *
 */
public class ShpNew {
	/**
	 * 读取shap格式的文件
	 * 
	 * @param path
	 * @throws ParseException 
	 */
	public static void readSHP(String inputPath_MP) throws ParseException {
		ShapefileDataStore shpDataStore = null;
		List<ShpFileBean> result1 = new ArrayList<ShpFileBean>();
		try {
			shpDataStore = new ShapefileDataStore(new File(inputPath_MP).toURI().toURL());
			shpDataStore.setStringCharset(Charset.forName("GBK"));
			// 文件名称
			String typeName = shpDataStore.getTypeNames()[0];
			FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
			featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) shpDataStore.getFeatureSource(typeName);
			FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
			SimpleFeatureType schema = result.getSchema(); // schema
			List<AttributeDescriptor> columns = schema.getAttributeDescriptors();
			FeatureIterator<SimpleFeature> itertor = result.features();
			/*
			 * 或者使用 FeatureReader FeatureReader reader =
			 * DataUtilities.reader(result); while(reader.hasNext()){
			 * SimpleFeature feature = (SimpleFeature) reader.next(); }
			 */
			boolean isStart = false;
			ShpFileBean sfb = null;
			while (itertor.hasNext()) {
				SimpleFeature feature = itertor.next();
				for (AttributeDescriptor attributeDes : columns) {
					String attributeName = attributeDes.getName().toString();// attribute
					System.out.println(attributeName);
					
					if (attributeName.equals("the_geom")) {
						sfb = new ShpFileBean();
						System.out.println(feature.getAttribute(attributeName));
						int srid = 4326;
						GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), srid);
						WKTReader reader = new WKTReader(geometryFactory);
						MultiPolygon mpolygon = (MultiPolygon) reader.read(feature.getAttribute(attributeName).toString());
						sfb.shpPoint = mpolygon;
						// Geometry geom = reader.read("POINT (" +
						// "118.1256612540001 24.442020778000074" + ")");
						// if (mpolygon.covers(geom)) {
						//
						// System.out.println(feature.getAttribute("hahahah"));
						// }
					}
					if (attributeName.equals("NO")) {
						Integer shpId = (Integer)feature.getAttribute(attributeName);
						sfb.shpId=shpId;
						System.out.println(feature.getAttribute(attributeName));
						result1.add(sfb);
					}
					feature.getAttribute(attributeName); // attributeValue
				}
			}
			itertor.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws ParseException {
		readSHP("D:\\mapinfoData\\xiamen\\xiamen_Shape\\xiamenjiaotongxiaoqu.SHP");
	}
}
